/*
 * Copyright (c) 2008, Justin Choy
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1) Redistributions of source code must retain the above copyright notice, 
 * this list of conditions and the following disclaimer.
 * 
 * 2) Redistributions in binary form must reproduce the above copyright notice, 
 * this list of conditions and the following disclaimer in the documentation 
 * and/or other materials provided with the distribution.
 * 
 * 3) Neither the name of the owner nor the names of its contributors may 
 * be used to endorse or promote products derived from this software without 
 * specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE.
 */
package data;

import java.util.ArrayList;

import error.InvalidValueException;
import error.ProgrammingErrorException;
import error.Severity;

/**
 * A Class implementation of a Node of a tree. It can store an arbitrary data
 * object as long as that object extends Comparable.
 * 
 * @author jchoy
 *
 * @param <T> The type of the data object to store
 */
public class Node< T extends Comparable<T> > {
    
    private ArrayList<Node<T>> childNodes = new ArrayList<Node<T>>(); 
    private Node<T> parentNode = null;
    
    private ArrayList<T> dataObjects = new ArrayList<T>();

    /**
     * Constructor
     * 
     * @param numDataObjects The number of data objects it can hold
     * @param numChildNodes The number of Child Nodes it can point to
     * @throws InvalidValueException 
     */
    public Node( int numDataObjects, int numChildNodes)
    throws ProgrammingErrorException, InvalidValueException {
        
        if ( numDataObjects < 0 ) {
            String s = "Node can not have a negative number of data objects.";
            throw new InvalidValueException( Severity.FATAL, s );
        }
        
        for ( int i = 0; i < numDataObjects; i++ ) {
            dataObjects.add( null );
        }
        
        if ( numChildNodes < 0 ) {
            String s = "Node can not have a negative number of children.";
            throw new InvalidValueException( Severity.FATAL, s );
        }

        for ( int i = 0; i < numChildNodes; i++ ) {
            childNodes.add( null );
        }

    }
    
    /**
     * @param index the index of the requested Data Object base 0
     * @return the dataObject stored in this Node
     */
    public T getDataObject( int index ) {
        return dataObjects.get( index );
    }
    
    /**
     * @param index the index where to set the data object
     * @param object the data object to set
     */
    public void setDataObject( int index, T object ) {
        dataObjects.set(  index, object );
    }
    
    /**
     * @param index the index of the requested child Node base 0
     * @return a reference to the requested child Node
     */
    public Node<T> getChildNode( int index ) {
        return childNodes.get( index );
    }
    
    /**
     * @param index The index of the requested child Node to set base 0
     * @param nextNode Sets the reference of requested child Node to nextNode
     */
    public void setChildNode( int index, Node<T> node ) {
        childNodes.set( index, node );
    }
    
    /**
     * @return a reference to Parent Node
     */
    public Node<T> getParentNode() {
        return parentNode;
    }
    
    /**
     * @param lastNode Sets the reference of the last Node to lastNode
     */
    public void setParentNode( Node<T> node ) {
        this.parentNode = node;
    }
    
}
